<?php
/**
 * Time.php
 * @author Meiko <sherlock.lynch@gmail.com>
 * @copyright Copyright (c) 2017
 * @version 2017/8/21
 */

namespace App\Utils;

class TimeUtil
{

    /**
     * get today begin and end
     * @return array
     */
    static function today()
    {
        $begin = mktime(0, 0, 0, date('m'), date('d'), date('Y'));
        $end = mktime(0, 0, 0, date('m'), date('d') + 1, date('Y')) - 1;
        return ['begin' => $begin, 'end' => $end];
    }

    /**
     * get yesterday begin and end
     * @return array
     */
    static function yesterday()
    {
        $begin = mktime(0, 0, 0, date('m'), date('d') - 1, date('Y'));
        $end = mktime(0, 0, 0, date('m'), date('d'), date('Y')) - 1;
        return ['begin' => $begin, 'end' => $end];
    }

    /**
     * get last week begin and end
     * @return array
     */
    static function lastWeek()
    {
        $begin = mktime(0, 0, 0, date('m'), date('d') - date('w') + 1 - 7, date('Y'));
        $end = mktime(23, 59, 59, date('m'), date('d') - date('w') + 7 - 7, date('Y'));
        return ['begin' => $begin, 'end' => $end];
    }

    /**
     * get this week begin and end
     * @return array
     */
    static function thisWeek()
    {
        $begin = mktime(0, 0, 0, date('m'), date('d') - date('w') + 1, date('Y'));
        $end = mktime(23, 59, 59, date('m'), date('d') - date('w') + 7, date('Y'));
        return ['begin' => $begin, 'end' => $end];
    }

    /**
     * get last month begin and end
     * @return array
     */
    static function lastMonth()
    {
        $begin = mktime(0, 0, 0, date('m') - 1, 1, date('Y'));
        $end = mktime(23, 59, 59, date('m'), 0, date('Y'));
        return ['begin' => $begin, 'end' => $end];
    }

    /**
     * get this month begin and end
     * @return array
     */
    static function thisMonth()
    {
        $begin = mktime(0, 0, 0, date('m'), 1, date('Y'));
        $end = mktime(23, 59, 59, date('m'), date('t'), date('Y'));
        return ['begin' => $begin, 'end' => $end];
    }

    /**
     * get this season begin and end
     * @return array
     */
    static function thisSeason()
    {
        $season = ceil(date('n') / 3);
        $begin = mktime(0, 0, 0, $season * 3 - 3 + 1, 1, date('Y'));
        $end = mktime(23, 59, 59, $season * 3, date('t', mktime(0, 0, 0, $season * 3, 1, date("Y"))), date('Y'));
        return ['begin' => $begin, 'end' => $end];
    }

    /**
     * get last season begin and end
     * @return array
     */
    static function lastSeason()
    {
        $season = ceil(date('n') / 3) - 1;
        $begin = mktime(0, 0, 0, $season * 3 - 3 + 1, 1, date('Y'));
        $end = mktime(23, 59, 59, $season * 3, date('t', mktime(0, 0, 0, $season * 3, 1, date("Y"))), date('Y'));
        return ['begin' => $begin, 'end' => $end];
    }

    /**
     * get this year begin and end
     * @return array
     */
    static function thisYear()
    {
        $begin = mktime(0, 0, 0, 1, 1, date('Y'));
        $end = mktime(23, 59, 59, 12, 31, date('Y'));
        return ['begin' => $begin, 'end' => $end];
    }

    /**
     * get this year begin and end
     * @return array
     */
    static function lastYear()
    {
        $begin = mktime(0, 0, 0, 1, 1, date('Y') - 1);
        $end = mktime(23, 59, 59, 12, 31, date('Y') - 1);
        return ['begin' => $begin, 'end' => $end];
    }

    /**
     * get begin end unix timestamp
     * @param string $code
     * @param array $period eg: ['1' => 'today', '2' => 'yesterday', '3' => 'thisWeek', '4' => 'lastWeek',
     *                          '5' => 'thisMonth','6' => 'lastMonth', '7' => 'thisSeason', '8' => 'lastSeason',
     *                          '9' => 'thisYear']
     * @return array
     */
    public static function getBeginEnd($code, $period)
    {

        $funcName = 'self::' . $period[$code];

        if (empty($period[$code])) {

            $result = self::today();
        } else {

            $result = call_user_func($funcName);
        }

        return [$result['begin'], $result['end']];

    }

    public static function diffDateTime($DateStart, $DateEnd)
    {
        $datetime1 = new \DateTime($DateStart);
        $datetime2 = new \DateTime($DateEnd);
        $interval = $datetime1->diff($datetime2);
        $time['y'] = $interval->format('%y');
        $time['m'] = $interval->format('%m');
        $time['d'] = $interval->format('%d');
        $time['h'] = $interval->format('%H');
        $time['i'] = $interval->format('%i');
        $time['s'] = $interval->format('%s');
        $time['a'] = $interval->format('%a'); // 两个时间相差总天数
        return $time;
    }


    /**
     * 判断时间格式是否正确
     * @param string $param 输入的时间
     * @param string $format 指定的时间格式
     * @return boolean
     */
    public static function isDatetime($param, $format = 'Y-m-d')
    {
        return date($format, strtotime($param)) === $param;
    }

    /**
     * 搜索默认时间
     * @param $search
     * @param int $period
     * @param string $start
     * @param string $end
     * @param string $dateType
     */
    public static function initPeriod(&$search, $period = 5, $start = 'start', $end = 'end', $dateType = 'dateType_start')
    {
        $periodInfo = self::getBeginEnd($period, config('FPConfig.FPConfig'));
        if (!isset($search[$start])) {
            $search[$start] = date('Y-m-d', $periodInfo[0]);
        }
        if (!isset($search[$end])) {
            $search[$end] = date('Y-m-d', $periodInfo[1]);
        }
        if (!isset($search[$dateType])) {
            $search[$dateType] = $period;
        }
    }

    /**
     * format date
     * @param $startDate
     * @return \DateTime|string
     */
    public static function formatStartDate($startDate)
    {
        $start = new \DateTime($startDate);
        $start = $start->format('Y-m-d H:i:s');
        return $start;
    }

    /**
     * format date 23:59:59
     * @param $endDate
     * @return \DateTime|string
     */
    public static function formatEndDate($endDate)
    {
        $end = new \DateTime($endDate . " 23:59:59");
        $end = $end->format('Y-m-d H:i:s');
        return $end;
    }

    public static function format($timestamp, $format = 'Y-m-d')
    {
        if (!$timestamp) {
            return '';
        }
        return date($format, $timestamp);
    }

    public static function timestampToDate($search, $timestamps = ['start', 'end'])
    {
        foreach ($timestamps as $timestamp) {
            if (isset($search[$timestamp]) && $search[$timestamp]) {
                $search[$timestamp] = self::format($search[$timestamp]);
            }
        }
        return $search;
    }

    /**
     * 如果是\DateTime格式化
     * @param $date
     * @param $format
     * @return string
     */
    public static function dateTimeFormat($date, $format = 'Y-m-d')
    {
        if ($date instanceof \DateTime) {
            return $date->format($format);
        }
        return $date;
    }
}
